package com.y2.authprj.mvc.controller.auth;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gdglc.spring.biz.auth.IRoleAuthorityBiz;
import com.gdglc.spring.biz.auth.ISysroleBiz;
import com.gdglc.spring.biz.auth.PageBiz;
import com.mysql.fabric.xmlrpc.base.Array;
import com.y2.authprj.bean.auth.model.FunctionauthorityInfo;
import com.y2.authprj.bean.auth.model.PagedResult;
import com.y2.authprj.bean.auth.model.RoleauthorityInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.bean.auth.vo.PageAuthority;
import com.y2.authprj.bean.auth.vo.PageSysrole;
import com.y2.authprj.bean.auth.vo.ShowRoleByMulitTableVo;
import com.y2.authprj.bean.auth.vo.showSysroleAndRoleAuthorityVo;

@Controller
@RequestMapping("/Sysrole")
public class SysroleController {
	@Resource(name="ISysroleBiz")
	private ISysroleBiz biz;
	@Resource(name="PageBiz")
	private PageBiz pageBiz;
	@Resource(name="IRoleAuthorityBiz")
	private IRoleAuthorityBiz authorityBiz;
	
	public ISysroleBiz getBiz() {
		return biz;
	}



	public void setBiz(ISysroleBiz biz) {
		this.biz = biz;
	}

	//页面跳转示例
	@RequestMapping("/index")
	public String index(Model model){
		List<SysroleInfo> sysroleInfos=biz.findAllSysrole();
		return "auth/index";
	}
	
	

	@RequestMapping("/show")
	public String show(Model model){
		List<SysroleInfo> sysroleInfos=biz.findAllSysrole();
		model.addAttribute("sysroleInfos", sysroleInfos);
		return "auth/SysRole2";
	}
	
	
	@RequestMapping("/page")
	public String page(PageSysrole sysrole,Model model,Integer pageNum){
		Integer pageSize=5;
		
		PagedResult<SysroleInfo> pageFun=pageBiz.querySysroleByPage(sysrole, pageNum, pageSize);
		model.addAttribute("sysroleInfos", pageFun.getDataList());
		model.addAttribute("funPage", pageFun);
		model.addAttribute("sysrole", sysrole);
		
		return "auth/SysRole";
	}
	
	@RequestMapping("/del")//@RequestParam(value="dat",required=false) 
	public String deleteSysrole(@RequestParam(value="datas[]") String[] datas){
		System.out.println("华哥");
		if(datas!=null){
			for(int i=0;i<datas.length;i++){
				int id=Integer.parseInt(datas[i].toString()) ;
				biz.deleteSysrole(id);
				authorityBiz.deleteRoleAuthorityByRoleId(id);
			}
		}
		
		return "redirect:/Sysrole/show";
	}
	
	
	@RequestMapping("/update")
	public String updateSysrole(SysroleInfo sysroleInfo,RedirectAttributes  redirect){
		int i=biz.updateSysrole(sysroleInfo);
		return "redirect:/Sysrole/show";
	}
	
	
	@RequestMapping("/add")
	public String addSysrole(SysroleInfo sysroleInfo,RedirectAttributes  redirect){
		int i=biz.addSysrole(sysroleInfo);
		return "redirect:/Sysrole/show";
	}
	
	@RequestMapping("/showAddBySysRoleId")
	public String showAddBySysRoleIdAuthority(@RequestParam(value="FunName",required=false) String funName,@RequestParam(value="roleId",required=false) Integer SysRoleId,Model model
			,Integer FunPageNum){
		Integer pageSize=5;
		FunctionauthorityInfo functionauthorityInfo=new FunctionauthorityInfo();
		PagedResult<FunctionauthorityInfo> pageFun=pageBiz.queryFunctionauthorityByPage(funName, FunPageNum, pageSize);
		functionauthorityInfo.setName(funName);
		model.addAttribute("functionauthorityInfo", functionauthorityInfo);
		model.addAttribute("funs", pageFun.getDataList());
		model.addAttribute("funPage", pageFun);
		
		SysroleInfo sysroleInfo=new SysroleInfo();
		sysroleInfo=biz.findSysroleByRoleId(SysRoleId);
		model.addAttribute("SysInfo", sysroleInfo);
		return "auth/AddAndUpdateRoleAuthority";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public List<showSysroleAndRoleAuthorityVo> showList(Integer SysRoleId){
		List<showSysroleAndRoleAuthorityVo> sysroleInfos=biz.findSysroleAndRoleAuthorityBySysId(SysRoleId);
		return sysroleInfos;
	}
	
	@RequestMapping("/delRoleAuthority")
	public String deleteSysrole(Integer RoleAuthorityId){
		authorityBiz.deleteRoleAuthority(RoleAuthorityId);
		return "redirect:/Sysrole/show";
	}

}
